package com.google.gwt.it2tool.client.widget;

import java.util.Iterator;

import com.google.gwt.it2tool.client.ClientDataCache;
import com.google.gwt.it2tool.client.ClientDataCache.DataChangeHandler;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;

public class ProductSuggestBox extends SuggestBox {
	
	private static MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();  
	private static ClientDataCache dataCache = ClientDataCache.getInstance();
	
	public ProductSuggestBox(){
		super(oracle);
		update();
	}
	
	public static void init(){
		dataCache.addDataChangeHandler(changeHandler);
	}
	
	private static void update(){
		oracle.clear();
		Iterator<String> iter = dataCache.getPrototypes().keySet().iterator();
		while(iter.hasNext()){
			oracle.add(iter.next());
		}
	}
	
	private static DataChangeHandler changeHandler = new DataChangeHandler() {
		
		@Override
		public void onRecipeDataChanged() {			
		}
		
		@Override
		public void onPrototypeDataChanged() {
			update();
		}
		
		@Override
		public void onMassProductionDataChanged() {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onFactoryDataChanged() {
			// TODO Auto-generated method stub
			
		}
	};

}
